package cn.stylefeng.guns.modular.system.controller;

import cn.stylefeng.guns.base.pojo.page.LayuiPageFactory;
import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo;
import cn.stylefeng.guns.modular.system.entity.DispatchOrder;
import cn.stylefeng.guns.modular.system.mapper.PurchaseOrdersMapper;
import cn.stylefeng.guns.modular.system.model.params.DispatchOrderParam;
import cn.stylefeng.guns.modular.system.model.params.PurchaseOrdersParam;
import cn.stylefeng.guns.modular.system.service.DispatchOrderService;
import cn.stylefeng.guns.modular.system.service.Myservice.DispathService;
import cn.stylefeng.guns.sys.core.shiro.ShiroKit;
import cn.stylefeng.guns.sys.modular.system.service.DeptService;
import cn.stylefeng.roses.core.base.controller.BaseController;
import cn.stylefeng.roses.core.page.PageFactory;
import cn.stylefeng.roses.core.reqres.response.ResponseData;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;


/**
 * 发货单控制器
 *
 * @author ZhangJun
 * @Date 2019-07-04 10:55:34
 */
@Controller
@RequestMapping("/dispatchOrder")
public class DispatchOrderController extends BaseController {

    private String PREFIX = "/dispatchOrder";

    @Autowired
    private DispatchOrderService dispatchOrderService;
    @Autowired
    private DispathService dispathService;
    @Autowired
    private  DeptService deptService;
    @Autowired
    private PurchaseOrdersMapper purchaseOrdersMapper;

    /**
     * 跳转到主页面
     *
     * @author ZhangJun
     * @Date 2019-07-04
     */
    @RequestMapping("")
    public String index() {
        return PREFIX + "/dispatchOrder.html";
    }

    /**
     * 新增页面
     *
     * @author ZhangJun
     * @Date 2019-07-04
     */
    @RequestMapping("/add")
    public String add() {
        return PREFIX + "/dispatchOrder_add.html";
    }

    /**
     * 编辑页面
     *
     * @author ZhangJun
     * @Date 2019-07-04
     */
    @RequestMapping("/edit")
    public String edit() {
        return PREFIX + "/dispatchOrder_edit.html";
    }


    /**
     * 跳转查看详情
     * @return
     */
    @RequestMapping("/dispatchDetail/{id}")
    public String detail(@PathVariable Long id){
        super.setAttr("id",id);
        super.setAttr("username", ShiroKit.getUserNotNull().getName());
        super.setAttr("deptName", deptService.getById(ShiroKit.getUserNotNull().getDeptId()).getSimpleName());
        return  PREFIX + "/dispatchOrderDetail.html";}


    /**
     * 新增接口
     *
     * @author ZhangJun
     * @Date 2019-07-04
     */
    @RequestMapping("/addItem")
    @ResponseBody
    public ResponseData addItem(DispatchOrderParam dispatchOrderParam) {
        this.dispatchOrderService.add(dispatchOrderParam);
        return ResponseData.success();
    }

    /**
     * 编辑接口
     *
     * @author ZhangJun
     * @Date 2019-07-04
     */
    @RequestMapping("/editItem")
    @ResponseBody
    public ResponseData editItem(DispatchOrderParam dispatchOrderParam) {
        this.dispatchOrderService.update(dispatchOrderParam);
        return ResponseData.success();
    }

    /**
     * 删除接口
     *
     * @author ZhangJun
     * @Date 2019-07-04
     */
    @RequestMapping("/delete")
    @ResponseBody
    public ResponseData delete(DispatchOrderParam dispatchOrderParam) {
        this.dispatchOrderService.delete(dispatchOrderParam);
        return ResponseData.success();
    }

    /**
     * 查看详情接口
     *
     * @author ZhangJun
     * @Date 2019-07-04
     */
    @RequestMapping("/detail")
    @ResponseBody
    public ResponseData detail(DispatchOrderParam dispatchOrderParam) {
        DispatchOrder detail = this.dispatchOrderService.getById(dispatchOrderParam.getId());
        return ResponseData.success(detail);
    }

    /**
     * 查询列表
     *
     * @author ZhangJun
     * @Date 2019-07-04
     */
    @ResponseBody
    @RequestMapping("/list")
    public LayuiPageInfo list(DispatchOrderParam dispatchOrderParam) {
        return this.dispatchOrderService.findPageBySpec(dispatchOrderParam);
    }

    /*发货单接口1.从前台获取发货数量 修改订货单中的数据，关联发货表，提交到相关部门去审核*/

    //1.生成发货单

    /**
     * 提交发货
     * @param purchaseOrdersParam
     * @return
     */
    @RequestMapping("/submit")
    public  ResponseData submit(PurchaseOrdersParam purchaseOrdersParam){
        ResponseData data = dispathService.submit(purchaseOrdersParam);
        return data;
    }


    @ResponseBody
    @RequestMapping("/getPurchase/{id}")
    public LayuiPageInfo getPurchaseByDispatch(@PathVariable Long id){

        Page page = PageFactory.defaultPage();
        DispatchOrder dispatchOrder = dispatchOrderService.getById(id);
        List<Long> ids = Arrays.asList(dispatchOrder.getPurchaseOrdersId().split(",")).stream().map((s) -> Long.parseLong(s)).collect(Collectors.toList());
        IPage pageUse = purchaseOrdersMapper.purchaseDetail(page,ids,ShiroKit.getUserNotNull().getId());
        return LayuiPageFactory.createPageInfo(pageUse);
    }



}


